<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工单编号" prop="taskCode">
        <el-input
          v-model="queryParams.taskCode"
          placeholder="请输入工单编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工单状态" prop="taskStatus">
        <el-select
          v-model="queryParams.taskStatus"
          placeholder="请选择工单状态"
          clearable
        >
          <el-option
            v-for="dict in task_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工单类型" prop="productTypeId">
        <el-select
          v-model="queryParams.productTypeId"
          placeholder="请选择工单类型"
          clearable
        >
          <el-option
            v-for="dict in taskTypeList"
            :key="dict.typeId"
            :label="dict.typeName"
            :value="dict.typeId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="taskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50" align="center" />
      <el-table-column label="工单编号" align="center" prop="taskCode" />
      <el-table-column label="设备编号" align="center" prop="innerCode" />
      <el-table-column label="工单类型" align="center" prop="taskType.typeName" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="地址" align="center" prop="addr" show-overflow-tooltip/>
      <el-table-column label="备注" align="center" prop="desc" show-overflow-tooltip/>
      <el-table-column label="工单状态" align="center" prop="taskStatus">
        <template #default="scope">
          <span :style="getStatusStyle(scope.row.taskStatus)">
            <dict-tag :options="task_status" :value="scope.row.taskStatus" />
          </span>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>


<script setup name="Task">
import { listTask,listTaskByUserId } from "@/api/manage/task";
import { getInfo } from '@/api/login'; 
import { loadAllParams } from '@/api/page'; // 用于加载参数
import { listTaskType } from '@/api/manage/taskType'; // 加载工单类型


const detailVisible = ref(false);
const taskId = ref('');
const userList = ref([]);
const taskTypeList = ref([]);

const { proxy } = getCurrentInstance();
const { task_status, task_create_type } = proxy.useDict('task_status', 'task_create_type');

const taskList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    taskCode: null,
    taskStatus: null,
    createType: null,
    innerCode: null,
    userId: null,
    userName: null,
    regionId: null,
    desc: null,
    productTypeId: null,
    assignorId: null,
    addr: null,
  },
  rules: {
    taskCode: [
      { required: true, message: "工单编号不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询工单列表 */
async function getList() {
  loading.value = true;

  // 获取用户信息
  const userInfo = await getInfo(); 

  let response;
  if (userInfo.user.admin) { // 检查用户是否为超级管理员
    response = await listTask(queryParams.value); // 管理员使用 listTask
  } else {
    queryParams.value.userId = userInfo.user.userId; // 设置用户ID
    response = await listTaskByUserId(userInfo.user.userId,queryParams.value); // 非管理员使用 listTaskByUserId
  }

  // 更新任务列表和总数
  taskList.value = response.rows;
  total.value = response.total;
  loading.value = false;
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    taskId: null,
    taskCode: null,
    taskStatus: null,
    createType: null,
    innerCode: null,
    userId: null,
    userName: null,
    regionId: null,
    desc: null,
    productTypeId: null,
    assignorId: null,
    addr: null,
    createTime: null,
    updateTime: null
  };
  proxy.resetForm("taskRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.taskId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

// 查询工单类型列表
function getTaskTypeList() {
  const page = {
    ...loadAllParams
  };
  listTaskType(page).then((response) => {
    taskTypeList.value = response.rows;
  });
}

const getStatusStyle = (status) => {
  switch (status) {
    case 1: // 待办
      return { color: '#FF6347' , fontWeight: 'bold' };
    case 2: // 进行
      return { color: '#0393EA' , fontWeight: 'bold'};
    case 3: // 取消
      return { color: '#FFD700' , fontWeight: 'bold'};
    case 4: // 完成
      return { color: '#32CD32' , fontWeight: 'bold'};
    default:
      return {};
  }
};


getTaskTypeList();
getList();
</script>
